package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.BatchQueryOrgDeptPositionListReq;
import com.sg.dto.base.framework.req.CheckRemovalObjsReq;
import com.sg.dto.base.framework.req.QueryOrgDeptPositionListReq;
import com.sg.entity.DeptPositionRelationship;
import com.sg.service.base.framework.MDeptPositionRelationshipService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.DeptPositionRelationshipMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MDeptPositionRelationshipServiceImpl
        extends ServiceImpl<DeptPositionRelationshipMapper, DeptPositionRelationship>
        implements MDeptPositionRelationshipService {

    @Autowired
    DeptPositionRelationshipMapper deptPositionRelationshipMapper;

    /**
     * 1-3-11新增组织部门职务[185]
     * gen by moon at 10/16/2022, 11:08:11 AM
     */
    @Trace(operationName = "1-3-11新增组织部门职务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addOrgDeptPosition(DeptPositionRelationship deptPositionRelationship) {
        deptPositionRelationship.setOrgPositionRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        deptPositionRelationship.setIsArchive("FALSE");
        deptPositionRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        deptPositionRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (deptPositionRelationship != null && deptPositionRelationship.getSubjectLifeCycle() != null && deptPositionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            deptPositionRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        deptPositionRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        deptPositionRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (deptPositionRelationship != null && deptPositionRelationship.getSubjectLifeCycle() != null && deptPositionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            deptPositionRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        deptPositionRelationship.setIsValid("FALSE");
        deptPositionRelationship.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        deptPositionRelationship.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(deptPositionRelationship)) {
            return deptPositionRelationship.getOrgPositionRelationshipId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-3-11修改部门岗位职责关系[187]
     * gen by moon at 10/16/2022, 11:08:12 AM
     */
    @Trace(operationName = "1-3-11修改部门岗位职责关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOrgDeptPositionRelationship(DeptPositionRelationship deptPositionRelationship) {

        return super.updateById(deptPositionRelationship);
    }

    /**
     * 1-3-11查询组织部门职务列表[193]
     * gen by moon at 10/16/2022, 11:08:13 AM
     */
    @Trace(operationName = "1-3-11查询组织部门职务列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DeptPositionRelationship> queryOrgDeptPositionList(QueryOrgDeptPositionListReq reqDto) {

        try {
            QueryWrapper<DeptPositionRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DeptPositionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<DeptPositionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-11查询部门岗位职务关系详情[194]
     * gen by moon at 10/16/2022, 11:08:13 AM
     */
    @Trace(operationName = "1-3-11查询部门岗位职务关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DeptPositionRelationship queryDeptPositionDetail(DeptPositionRelationship deptPositionRelationship) {

        try {

            QueryWrapper<DeptPositionRelationship> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(deptPositionRelationship);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<DeptPositionRelationship> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                DeptPositionRelationship retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-11修改部门岗位职务关系[907]
     * gen by moon at 10/16/2022, 11:08:14 AM
     */
    @Trace(operationName = "1-3-11修改部门岗位职务关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDeptPosition(DeptPositionRelationship deptPositionRelationship) {
        deptPositionRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        deptPositionRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(deptPositionRelationship);
    }

    /**
     * 1-3-11批量修改部门岗位职务关系[932]
     * gen by moon at 10/16/2022, 11:08:15 AM
     */
    @Trace(operationName = "1-3-11批量修改部门岗位职务关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateDeptPosition(List<DeptPositionRelationship> deptPositionRelationship) {

        if (deptPositionRelationship == null || deptPositionRelationship.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(deptPositionRelationship);

    }

    /**
     * 1-3-11批量删除部门岗位职务关系[941]
     * gen by moon at 10/16/2022, 11:08:15 AM
     */
    @Trace(operationName = "1-3-11批量删除部门岗位职务关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteDeptPosition(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-3-11批量新增部门岗位职务关系[1497]
     * gen by moon at 10/16/2022, 11:08:16 AM
     */
    @Trace(operationName = "1-3-11批量新增部门岗位职务关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddDeptPosition(List<DeptPositionRelationship> deptPositionRelationship) {

        if (deptPositionRelationship == null || deptPositionRelationship.size() == 0) {
            return false;
        }

        return super.saveBatch(deptPositionRelationship.stream().map(item -> {
            item.setOrgPositionRelationshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsArchive("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-3-11删除部门岗位职务关系[1503]
     * gen by moon at 10/16/2022, 11:08:17 AM
     */
    @Trace(operationName = "1-3-11删除部门岗位职务关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDeptPosition(String orgPositionRelationshipId) {

        return super.removeById(orgPositionRelationshipId);
    }

    /**
     * 1-3-11批量查询组织部门职务列表[1600]
     * gen by moon at 10/16/2022, 11:08:18 AM
     */
    @Trace(operationName = "1-3-11批量查询组织部门职务列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DeptPositionRelationship> batchQueryOrgDeptPositionList(BatchQueryOrgDeptPositionListReq reqDto) {

        try {
            QueryWrapper<DeptPositionRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getOrgDeptPositionList() == null || reqDto.getOrgDeptPositionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("position_id", reqDto.getOrgDeptPositionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DeptPositionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<DeptPositionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-11修改组织部门职务[1641]
     * gen by moon at 10/16/2022, 11:08:18 AM
     */
    @Trace(operationName = "1-3-11修改组织部门职务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOrgDeptPosition(DeptPositionRelationship deptPositionRelationship) {
        deptPositionRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (deptPositionRelationship != null && deptPositionRelationship.getSubjectLifeCycle() != null && deptPositionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            deptPositionRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (deptPositionRelationship != null && deptPositionRelationship.getSubjectLifeCycle() != null && deptPositionRelationship.getSubjectLifeCycle().equals("ARCHIVING")) {
            deptPositionRelationship.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        deptPositionRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (deptPositionRelationship != null && deptPositionRelationship.getSubjectLifeCycle() != null && deptPositionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            deptPositionRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (deptPositionRelationship != null && deptPositionRelationship.getSubjectLifeCycle() != null && deptPositionRelationship.getSubjectLifeCycle().equals("ARCHIVING")) {
            deptPositionRelationship.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(deptPositionRelationship);
    }

    /**
     * 1-3-11批量删除组织部门职务[1666]
     * gen by moon at 10/16/2022, 11:08:19 AM
     */
    @Trace(operationName = "1-3-11批量删除组织部门职务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteOrgDeptPosition(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-3-11批量修改组织部门职务[1667]
     * gen by moon at 10/16/2022, 11:08:20 AM
     */
    @Trace(operationName = "1-3-11批量修改组织部门职务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateOrgDeptPosition(List<DeptPositionRelationship> deptPositionRelationship) {

        if (deptPositionRelationship == null || deptPositionRelationship.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(deptPositionRelationship);

    }

    /**
     * M去重职务所属对象（特殊方法）[2174]
     */
    @Trace(operationName = "M去重职务所属对象（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DeptPositionRelationship> checkRemovalObjs(CheckRemovalObjsReq reqDto) {

        try {
            QueryWrapper<DeptPositionRelationship> queryWrapper = new QueryWrapper<>();

            queryWrapper.in("org_position_relationship_id", reqDto.getOrgDeptPositionList().stream().map(OrgDeptPositionDto::getOrgPositionRelationshipId).collect(Collectors.toList()));
            queryWrapper.in("org_entity_id", reqDto.getOrgDeptPositionList().stream().map(OrgDeptPositionDto::getOrgEntityId).collect(Collectors.toList()));
            queryWrapper.in("operation_induction_id", reqDto.getOrgDeptPositionList().stream().map(OrgDeptPositionDto::getOperationInductionId).collect(Collectors.toList()));
            queryWrapper.in("operate_time", reqDto.getOrgDeptPositionList().stream().map(OrgDeptPositionDto::getOperateTime).collect(Collectors.toList()));
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<DeptPositionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-11批量增组织部门职务[3769]
     * gen by moon at 10/16/2022, 11:08:20 AM
     */
    @Trace(operationName = "1-3-11批量增组织部门职务")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddOrgDeptPosition(List<DeptPositionRelationship> deptPositionRelationship) {

        if (deptPositionRelationship == null || deptPositionRelationship.size() == 0) {
            return false;
        }

        return super.saveBatch(deptPositionRelationship.stream().map(item -> {
            item.setIsArchive("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * M1查询组织部门职务关系测试列表[4899]
     * gen by moon at 10/16/2022, 11:08:21 AM
     */
    @Trace(operationName = "M1查询组织部门职务关系测试列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DeptPositionRelationship> queryPositionRelationshipTestDataList(DeptPositionRelationship deptPositionRelationship) {

        try {
            QueryWrapper<DeptPositionRelationship> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(deptPositionRelationship);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DeptPositionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DeptPositionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<DeptPositionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删组织部门职务关系测试[4900]
     * gen by moon at 10/16/2022, 11:08:22 AM
     */
    @Trace(operationName = "M1批量删组织部门职务关系测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePositionRelationshipTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

}

